import logging
import configparser
from pathlib import Path

from messenger.config.settings import Config
from messenger.utils.redis_util import RedisClient


class ConsumerApp():
    instance = None
    init_flat = False

    def __new__(cls, *args, **kwargs):
        if cls.instance is None:
            cls.instance = super().__new__(cls)
        return cls.instance

    def __init__(self):
        if self.init_flat is True:
            return
        self.config = self.init_config()
        self.init_flat = True

    @property
    def logger(self):
        return logging

    @staticmethod
    def init_config(path=Config.INI_PATH):
        config = dict()
        if not path:
            return False

        consumer_config_ini = Path(path)

        cfg = configparser.ConfigParser()
        cfg.read(consumer_config_ini)

        for section, _ in cfg.items():
            for key, value in cfg.items(section):
                try:
                    _value = int(value)

                except ValueError:
                    _value = value

                config[key.upper()] = _value

        return config


consumer_app = ConsumerApp()
redis_client = RedisClient()
redis_client.init_app(consumer_app)
